﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestGenerator.Input;

namespace TestGenerator.Generator
{
    class TestBuilder
    {
        private static TestResource resource;
        private static Random random = new Random();

        public static TestResource Resource
        {
            get { return TestBuilder.resource; }
            set { TestBuilder.resource = value; }
        }

        static TestBuilder()
        {
            Resource = new TestResource();
        }

        public TestBuilder()
        {
        }

        public static  List<GeneratedQuestion> BuildCompleteTest()
        {
            List<Question> allQuestions = resource.Questions;
            List<GeneratedQuestion> completeTest = new List<GeneratedQuestion>();
            int maxQuestions = TestResource.Initialize.MaxQuestions;
            int numOfAllquestions = Resource.Questions.Count;

            int swapPosition = numOfAllquestions - 1;

            for (int i = 0; i < maxQuestions; i++)
            {            
                    int qPosition = random.Next(swapPosition + 1);
                    GeneratedQuestion currGeneratedQuestion = BuildCompleteQuestion(allQuestions[qPosition]);
                    completeTest.Add(currGeneratedQuestion);

                    Question toSwap = allQuestions[qPosition];
                    allQuestions[qPosition] = allQuestions[swapPosition];
                    allQuestions[swapPosition] = toSwap;
                    swapPosition--;
            }
            return completeTest;
        }

        public static GeneratedQuestion BuildCompleteQuestion(Question questionInfo)
        {
            int correctAnswers = questionInfo.Correct.Length;
            int wrongAnswers = questionInfo.Wrong.Length;

            if (correctAnswers == 0 || wrongAnswers == 0)
            {
                throw new ArgumentException("Every Question must have at least 1 worng and 1 correct answer! ");
            }

            int maxAns = Math.Min(TestResource.Initialize.MaxAnswers, wrongAnswers + 1);
            List<string> ansList = new List<string>();

   
            int correctAnsIndex = random.Next(correctAnswers);
            ansList.Add( questionInfo.Correct[correctAnsIndex]);

            string[] wrongAns = questionInfo.Wrong;
            int swapPosition = wrongAnswers -1;
            int k = wrongAnswers;

            for (int i = 1; i < maxAns; i++)
            {
                int wrongIndex = random.Next(1,k);
                string currWrong = wrongAns[wrongIndex];
                ansList.Add(currWrong);

                string toSwap = currWrong;
                wrongAns[wrongIndex] = wrongAns[swapPosition];
                wrongAns[swapPosition] = toSwap;
                swapPosition--;
                k--;
                
            }

            string toSwapp = ansList[0];
            int swapPos = random.Next(maxAns);
            correctAnsIndex = swapPos;
            ansList[0] = ansList[swapPos ];
            ansList[swapPos] = toSwapp;
           


            GeneratedQuestion newQuestion = new GeneratedQuestion(questionInfo.QuestionText, correctAnsIndex, ansList);
            return newQuestion;
        }

  

    }
}
